package com.example.myapplication.tack_07.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.tack_07.XQActivity;
import com.example.myapplication.tack_07.data.ListData;
import com.example.myapplication.utils.Common;
import com.example.myapplication.utils.OkHttpTool;
import java.util.List;

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.My> {
    private List<ListData.RowsDTO> list;
    private Context context;
    private View view;
    private int cou = 1;
    public ListAdapter(List<ListData.RowsDTO> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public My onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(context).inflate(R.layout.youth_list,null);
        return new My(view);
    }

    @Override
    public void onBindViewHolder(@NonNull My holder, int position) {
        Glide.with(context).load(OkHttpTool.URL + list.get(position).getCoverImgUrl()).into(holder.imags);
        holder.title.setText(list.get(position).getName());
        holder.content.setText("剩余床位："+list.get(position).getBedsCountBoy()+"(男)"+
                list.get(position).getBedsCountGirl()+"(女)");
        holder.commentNum.setText(list.get(position).getAddress());
        holder.publishDate.setText("点击查看更多");
        holder.publishDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cou++;
                if(cou%2==0){
                    holder.introduce.setVisibility(View.VISIBLE);
                    holder.introduce.setText("    详情：\n\n     "+list.get(position).getIntroduce());
                }else{
                    holder.introduce.setVisibility(View.GONE);
                    holder.introduce.setText("    详情：\n\n     "+list.get(position).getIntroduce());
                }
            }
        });
        holder.list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, XQActivity.class);
                String id = String.valueOf(list.get(position).getId());
                intent.putExtra(Common.COMMON,id);
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return list != null?list.size():0;
    }

    public class My extends RecyclerView.ViewHolder{
        private ImageView imags;
        private TextView title;
        private TextView content;
        private TextView commentNum;
        private TextView publishDate;
        private TextView introduce;
        private LinearLayout list;
        public My(@NonNull View itemView) {
            super(itemView);
            imags = view.findViewById(R.id.imags);
            title = view.findViewById(R.id.title);
            content = view.findViewById(R.id.content);
            commentNum = view.findViewById(R.id.commentNum);
            publishDate = view.findViewById(R.id.publishDate);
            introduce = view.findViewById(R.id.introduce);
            list = view.findViewById(R.id.list);
        }
    }
}
